﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
//
using Mmht.BT;

public partial class admin_phsj_sctp : System.Web.UI.Page
{
    static public ArrayList hif = new ArrayList();
    //保存文件列表     
    public int filesUploaded = 0;
    //上传文件的数量  
    string message = string.Empty;
    int create_by;
    DateTime create_at;
    public string picPath1;
    public string picPath2;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        try
        {
            create_by = (Int32)Session["admin_id"];
        }
        catch
        {
            create_by = 2;
        }
        picPath1 = Server.MapPath("files/heat_treating_data");
        picPath2 = Server.MapPath("files/structure_property");
    }

    //-----------------添加列表  
    protected void AddFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
        //这个很重要哦．呵呵．按键在第一次ＰＡＧＥ加载后才起作用        
        {
            if (FindFile.HasFile)  //判断浏览文本框中有没有文件内容        
            {
                hif.Add(FindFile);
                //把文件完整路径添加到数组中去   
                FileList.Items.Add(System.IO.Path.GetFileName(FindFile.PostedFile.FileName));
                //获取一个待上传的文件名        
            }
            else
            {
                message = "错误 - 必须要有上传的pdf文件.";
                message_show();
            }
        }
        else { }
    }

    ///从listbox中删除指定的文件 
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)  //if the Listbox's item is not selected  
        {
            message = "错误 - 必须指定要删除的文件.";
            message_show();
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            try
            {
                hif.RemoveAt(FileList.SelectedIndex);
                //Remove the item which is selected from the ArryList                                 
            }
            catch
            {

            }
            FileList.Items.Remove(FileList.SelectedItem.Text);
            //Remove the item which is selected in the listbox.     
        }
    }

    /// <summary>    
    /// 
    /// 循环上传listbox中的文件到指定的文件夹下     
    /// </summary>    
    ///
    /// <param name="sender"></param>     
    /// <param name="e"></param>  
    protected void Upload_Click(object sender, EventArgs e)
    {
        //delete_file(DropDownList1.SelectedItem.Value,"image");
        string url0 = "files/" + DropDownList1.SelectedItem.Value + "/"; //文件路径//files/pdf/bz/GB_1221-1992耐热钢棒.pdf
        string type = DropDownList1.SelectedItem.Text;//文件类型
        create_at = DateTime.Now;

        string LocalPath = Server.MapPath(url0); // 上传路径  

        string message2 = string.Empty;
        if ((FileList.Items.Count == 0) && (filesUploaded == 0)) //judge the listbox'item is nothing        
        {
            message = "错误 - 必须指定要上传的图片文件.";
            message_show();
            return;
        }
        else
        {
            this.FileListupload.Items.Clear();
            foreach (System.Web.UI.WebControls.FileUpload HIF in hif)
            {
                try
                {
                    string fileExtension = System.IO.Path.GetExtension(HIF.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg",".bmp" };
                    string fn = string.Empty;
                    string fn0 = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                    fn = fn0.Replace(" ", "");
                    bool fileOK = false;
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                            break;
                        }
                        else
                        {
                            
                        }
                    }
                    if (fileOK)
                    {
                        HIF.PostedFile.SaveAs(LocalPath + fn);
                        filesUploaded++;
                        FileListupload.Items.Add(fn);
                        FileList.Items.Remove(fn0);
                    }
                    else
                    {
                        message2 += fn + ",";
                        //message_show();
                        FileList.Items.Remove(fn0);
                        //FileList.Items.Clear();
                    }
                }
                catch (Exception err)
                {
                    message += "上传错误 " + LocalPath + "" + err.ToString() + ".";
                    //message_show();
                }
            }
            message += "共上传成功了 " + filesUploaded + " 个图片文件.";
            if (message2.Length > 1)
            {
                message += "文件：" + message2.Substring(0, message2.Length - 1) + "格式错误，上传失败！";
            }
            message_show();
            hif.Clear();
            //FileList.Items.Clear();
        }
    }

    private void message_show()
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script language='javascript'>  warning('" + message + "');</script>");
    }

    private void delete_file(string content,string type)
    {
        if (Cb_Del.Checked)
        {
            FileDelete fd = new FileDelete(content,type);
            fd.file_delete();
            Cb_Del.Checked = false;
        }
    }
}
